#include <stdio.h>
#include <stdlib.h>
#include "../tools/tools.h"
#include "../tools/msg_tool.h"

void usage(char* src) {
	fprintf(stderr,"Usage: %s <input> <output> <-m msg | -f file> [-n len]", src);
	exit(-2);
}

int main(int argc, char** argv) {
	char* msg = NULL;
	long msglen = 0;
	int ret;
    if (argc < 5)
		usage(argv[0]);

  if (strcmp(argv[3],"-m") == 0) {
	  msglen = strlen(argv[4]);
	  msg = (char*) malloc((msglen+1)*sizeof(char));
	  strncpy(msg, argv[4], msglen);
	  msg[msglen] = '\0';
  } else {
	  msglen = readFile(argv[4], &msg);
	  if (msglen < 0)
		  exit(msglen);
  }

  // length is stored in msglen, not end with '\0'
  if (argc >= 7) {
	  if (strcmp(argv[5],"-n") == 0) {
		  msglen = atoi(argv[6]);
		  if (msglen <= 0) {
			  fprintf(stderr, "invalid length specified.\n");
			  usage(argv[0]);
		  }
	  }
	  else {
		  usage(argv[0]);
	  }
  }

  //ret = jstege(argv[1],argv[2],msg,msglen, 0);
  //ret = f3e(argv[1],argv[2],msg,msglen, 0);
  ret = f3e(argv[1],argv[2],msg,msglen, 0);
  free(msg);
  return ret;
}